/**
 * com.yhte.mir.dao.impl
 * CenterDaoImpl.java
 * sunsu
 * 2012-9-18
 */
package com.yhte.mir.center.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.yhte.mir.center.dao.CenterDao;
import com.yhte.mir.center.vo.CenterInfo;
import com.yhte.mir.center.vo.ClientLink;
import com.yhte.mir.ill.vo.IllInfo;
import com.yhte.mir.util.HpysConveter;

/**
 * @author sunsu
 *
 */
public class CenterDaoImpl extends SqlMapClientTemplate implements CenterDao {
	private static Logger log = Logger.getLogger(CenterDaoImpl.class.getName());
	
	public List<ClientLink> findDataLink() {
		try {
			return this.getSqlMapClient().queryForList("findDataLink");
		} catch (SQLException e) {
			log.error("查询前端datalink失败："+e.getMessage());
			return null;
		}
	}

	/* (non-Javadoc)
	 * 交管多次违法库同步到本地(先删后插)
	 */
	public void insertHmd(List<IllInfo> illInfos) {
		try {
			this.getSqlMapClient().update("delCenterHmd");
			Connection conn = this.getSqlMapClient().getDataSource().getConnection();
			conn.setAutoCommit(false);
			String sql = "insert into CENTER_HMD_VEHICLE_DETAIL" +
					"(clbh,hphm,hpys,bz,rktj) values (?,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			for(int i=0;i<illInfos.size();i++){
				IllInfo info = illInfos.get(i);
				ps.setLong(1, info.getClbh());
				ps.setString(2, info.getHphm());
				ps.setString(3, HpysConveter.hpysMap.get(info.getHpys()));
				ps.setString(4, info.getBz());
				ps.setString(5, info.getRktj());
				ps.addBatch();
				if(i%500==0 && i!=0){
					ps.executeBatch();
					ps.clearBatch();
				}
			}
			ps.executeBatch();
			ps.clearBatch();
			conn.commit();
		} catch (SQLException e) {
			log.error("交管多次违法库同步到本地出错："+e.getMessage());
			e.printStackTrace();
		}
		
	}

	@Override
	public List<IllInfo> findAllHmd(String link) {
		try {
			return this.getSqlMapClient().queryForList("findAllHmd",link);
		} catch (SQLException e) {
			log.error("查询多次违法记录车辆失败：："+e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<CenterInfo> findDupHphm(String link) {
		try {
			return this.getSqlMapClient().queryForList("findDupHphm",link);
		} catch (SQLException e) {
			log.error("查找前端在交管库中没有的多次违法车辆失败:"+e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void delDupHphm(String link,List dupHphms) {
		Map map = new HashMap();
		map.put("link", link);
		map.put("data", dupHphms);
		try {
			this.getSqlMapClient().delete("delClientDupHphm",map);
		} catch (SQLException e) {
			log.error("删除前端在交管库中没有的多次违法车辆失败:"+e.getMessage());
			e.printStackTrace();
		}
		try {
			this.getSqlMapClient().delete("delCenterDupHphm",map);
		} catch (SQLException e) {
			log.error("删除本地在交管库中没有的多次违法车辆失败:"+e.getMessage());
			e.printStackTrace();
		}
	}

}
